package com.audi.activity;

import java.util.ArrayList;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.audi.R;
import com.audi.BaseActivity;
import com.audi.adapter.SQLiteAsusNewHelper;
import com.audi.adapter.StatusDevicesAdapter;
import com.audi.dto.DeviceItem;
import com.audi.utils.Constants;

@SuppressLint("Registered")
public class StatusDevicesActivity extends BaseActivity {
    private ListView mLstStatusDevices;
    private StatusDevicesAdapter adapterStatusDevices;
    private ArrayList<DeviceItem> mArrStatusDevices;
    private SQLiteAsusNewHelper mSqlite;
   /* private SceneItem itemScene;
    private PlaceItem itemPlace;*/
    private TextView mTxtSceneName, mTxtPlaceName;
    private int placeId, sceneId;
    private String placeName, sceneName;
    private ImageView mBtnDone, mBtnCancel;
    private DeviceItem itemDevices;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
	// TODO Auto-generated method stub
	super.onCreate(savedInstanceState);
	setContentView(R.layout.activity_status_devices);
	initActionBar(R.layout.action_bar_status_devices);
	setDisableDrawer();
	mLstStatusDevices = (ListView) findViewById(R.id.lst_status_devices);
	mTxtSceneName=(TextView)findViewById(R.id.txt_scene_name);
	mTxtPlaceName=(TextView)findViewById(R.id.txt_place_name);
	mBtnDone=(ImageView)findViewById(R.id.btn_status_done);
	mBtnCancel=(ImageView)findViewById(R.id.btn_status_cancel);
	itemDevices=new DeviceItem();
	mLstStatusDevices.setOnItemClickListener(new OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int position,
				long arg3) {
			// TODO Auto-generated method stub
			itemDevices=mArrStatusDevices.get(position);
			if(itemDevices.getDeviceStatus()==Constants.CONTROL_ON_OFF.ON.ordinal()) {
				itemDevices.setDeviceStatus(Constants.CONTROL_ON_OFF.OFF.ordinal());
				mSqlite.updateDeviceStatus(Constants.CONTROL_ON_OFF.ON.ordinal(), Constants.CONTROL_ON_OFF.OFF.ordinal());
			}
			else if(itemDevices.getDeviceStatus()==Constants.CONTROL_ON_OFF.OFF.ordinal()) {
				itemDevices.setDeviceStatus(Constants.CONTROL_ON_OFF.ON.ordinal());
				mSqlite.updateDeviceStatus(Constants.CONTROL_ON_OFF.OFF.ordinal(), Constants.CONTROL_ON_OFF.ON.ordinal());
			}
			adapterStatusDevices.notifyDataSetChanged();
		}
	});
	
	mBtnCancel.setOnClickListener(new OnClickListener() {
		
		@Override
		public void onClick(View arg0) {
			// TODO Auto-generated method stub
			/*Intent intentPlaceCardDetail=new Intent(StatusDevicesActivity.this, PlaceCardDetailActivity.class);
			startActivity(intentPlaceCardDetail);*/
			finish();
		}
	});
	
	mBtnDone.setOnClickListener(new OnClickListener() {
		
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			finish();
		}
	});
	
	mSqlite=new SQLiteAsusNewHelper(this);
	
	Bundle bundleStatusDevices = getIntent().getExtras();
	if (bundleStatusDevices != null) {	    
	    placeId = bundleStatusDevices.getInt(Constants.DATA_ID_PLACE);
	    placeName = bundleStatusDevices.getString(Constants.DATA_NAME_PLACE);
	    sceneId=bundleStatusDevices.getInt(Constants.DATA_ID_SCENE);
	    sceneName=bundleStatusDevices.getString(Constants.DATA_NAME_SCENE);
	    
	    mTxtPlaceName.setText(placeName);
		mTxtSceneName.setText(sceneName);
		mArrStatusDevices=mSqlite.getAllDevice(placeId);
		adapterStatusDevices = new StatusDevicesAdapter(this, mArrStatusDevices);
		mLstStatusDevices.setAdapter(adapterStatusDevices);
	}
	
	/*Intent intent=getIntent();
	Bundle bundle=intent.getBundleExtra("Data");
	itemScene=(SceneItem) bundle.getSerializable("Scene");
	itemPlace=(PlaceItem) bundle.getSerializable("Place");
	mTxtPlaceName.setText(itemPlace.getPlaceName().toString());
	mTxtSceneName.setText(itemScene.getSceneTitle().toString());
	mArrStatusDevices=mSqlite.getAllDevice(itemPlace.getPlaceId());*/
	/*mArrStatusDevices.add(new DeviceItem("On/Off device", "",
		"%place_name%", 1, 1));
	mArrStatusDevices.add(new DeviceItem("On/Off device", "",
		"%place_name%", 0, 1));
	mArrStatusDevices.add(new DeviceItem("Warning sensor", "",
		"%place_name%", -1, 0));
	mArrStatusDevices.add(new DeviceItem("Temperature", "", "%place_name%",
		-1, 0));*/

	
    }

	@Override
	public void onRefreshData() {
		// TODO Auto-generated method stub
		
	}

	
}
